public class AVLTree {

    static class TreeNode {
        public int val;
        public int bf;//平衡因子
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右子树的引用
        public TreeNode parent;//父亲节点的引用

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;//根节点
    public boolean insert(int val){
        TreeNode node = new TreeNode(val);
        if(root == null){
            root = node;
            return true;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while(cur != null){
            if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else {
                return false;
            }
        }
        if(parent.val > val){
            parent.left = node;
        }else {
            parent.right = node;
        }
        node.parent = parent;
        cur = node;
        while(parent != null){
            if(parent.left == cur){
                parent.bf--;
            }else {
                parent.bf++;
            }
            if(parent.bf == 0){
                break;
            }else if(parent.bf == -1 || parent.bf == 1){
                cur = parent;
                parent = parent.parent;
            }else {
                if(parent.bf == 2){
                    if(cur.bf == 1){
                        rotateLeft(parent);
                    }else {
                        rotateRL(parent);
                    }
                }else{
                    if(cur.bf == -1){
                        rotateRight(parent);
                    }else {
                        rotateLR(parent);
                    }
                }
                break;
            }
        }
        return true;
    }
    //右左双旋
    private void rotateRL(TreeNode parent) {
        TreeNode subR = parent.right;
        TreeNode subRL = subR.left;
        int bf = subRL.bf;
        rotateRight(parent.right);
        rotateLeft(parent);
        if(bf == -1) {
            subR.bf = 1;
            subRL.bf = 0;
            parent.bf = 0;
        }else if(bf == 1){
            subR.bf = 0;
            subRL.bf = 0;
            parent.bf = -1;
        }
    }
    //左右双旋
    private void rotateLR(TreeNode parent) {
        TreeNode subL = parent.left;
        TreeNode subLR = subL.right;
        int bf = subLR.bf;
        rotateLeft(parent.left);
        rotateRight(parent);
        if(bf == -1) {
            subL.bf = 0;
            subLR.bf = 0;
            parent.bf = 1;
        }else if(bf == 1){
            subL.bf = -1;
            subLR.bf = 0;
            parent.bf = 0;
        }
    }
    //左旋
    private void rotateLeft(TreeNode parent){
        TreeNode subR = parent.right;
        TreeNode subRL = subR.left;
        TreeNode Pparent = parent.parent;
        parent.right = subRL;
        subR.left = parent;
        if(subRL != null) {
            subRL.parent = parent;
        }
        parent.parent = subR;
        if(Pparent == null){
            root = subR;
            root.parent = null;
        }else {
            if(Pparent.left == parent){
                Pparent.left = subR;
            }else {
                Pparent.right = subR;
            }
            subR.parent = Pparent;
        }
        subR.bf = parent.bf = 0;
    }
    //右旋
    private void rotateRight(TreeNode parent) {
        TreeNode subL = parent.left;
        TreeNode subLR = subL.right;
        TreeNode Pparent = parent.parent;
        parent.left = subLR;
        subL.right = parent;
        if(subLR != null){
            subLR.parent = parent;
        }
        parent.parent = subL;
        if(Pparent == null){
            root = subL;
            root.parent = null;
        }else {
            if(Pparent.left == parent){
                Pparent.left = subL;
            }else {
                Pparent.right = subL;
            }
            subL.parent = Pparent;
        }
        subL.bf = parent.bf = 0;
    }
    //中序遍历的结果是有序的 就能说明当前树 一定是AVL树吗？  不一定的
    public void inorder(TreeNode root) {
        if(root == null) return;
        inorder(root.left);
        System.out.print(root.val+"  ");
        inorder(root.right);
    }

    private int height(TreeNode root) {
        if(root == null) return 0;
        int leftH = height(root.left);
        int rightH = height(root.right);

        return leftH > rightH ? leftH+1 : rightH+1;
    }

    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        int leftH = height(root.left);
        int rightH = height(root.right);

        if(rightH-leftH != root.bf) {
            System.out.println("这个节点："+root.val+" 平衡因子异常");
            return false;
        }

        return Math.abs(leftH-rightH) <= 1
                && isBalanced(root.left)
                && isBalanced(root.right);
    }
}